package com.Trainer.TrainerInterface;

import com.Trainer.API;
import com.data.TrainerData;
import com.data.UpData;

import javax.swing.*;
import java.util.Objects;

public class TrainerInterface extends JPanel {
    //属性
    boolean isJudged = true;
    TrainerData trainerData;
    UpData upData;
    TrainerUpPanel trainerUpPanel;
    TrainerBottomRightPanel trainerBottomRightPanel;
    TrainerBottomLeftPanel trainerBottomLeftPanel;
    String patten = "";//记录当前模式

    //构造器
    public TrainerInterface(int width, int height){
        trainerData=new TrainerData();
        upData=new UpData();
        trainerUpPanel= new TrainerUpPanel(0,0,900,100,upData);
        trainerBottomRightPanel=new TrainerBottomRightPanel(650,100,250,400);
        trainerBottomLeftPanel = new TrainerBottomLeftPanel(0, 100, 650, 400,trainerData);
        //布局
        setBounds(0,0,width,height);
        setLayout(null);
        add(trainerUpPanel);
        add(trainerBottomLeftPanel);
        add(trainerBottomRightPanel);

        //事件
        trainerBottomRightPanel.getPlusBtn().addActionListener((e)->{
            trainerUpPanel.getCurrentPattern().setText("加法模式");
            patten = "加法";
            isJudged=false;
            API.getNext(patten,upData,trainerData,trainerBottomLeftPanel,trainerUpPanel);
        });

        trainerBottomRightPanel.getSubBtn().addActionListener((e)->{
            trainerUpPanel.getCurrentPattern().setText("减法模式");
            patten = "减法";
            isJudged=false;
            API.getNext(patten,upData,trainerData,trainerBottomLeftPanel,trainerUpPanel);
        });

        trainerBottomRightPanel.getPlusSubBtn().addActionListener((e)->{
            trainerUpPanel.getCurrentPattern().setText("加减混合模式");
            patten = "加减法";
            isJudged=false;
            API.getNext(patten,upData,trainerData,trainerBottomLeftPanel,trainerUpPanel);
        });
        //返回按钮的事件由于需要关联其他panel，所以在myTrainer中实现

        trainerBottomLeftPanel.getConfirmBtn().addActionListener((e)->{
            int submit = trainerBottomLeftPanel.submit();//获取答案
            if (!isJudged){
                compare(submit);
                isJudged=true;
            }
        });

        trainerBottomLeftPanel.getNextBtn().addActionListener((e)->{
            isJudged=false;
            if(!patten.equals(""))  API.getNext(patten,upData,trainerData,trainerBottomLeftPanel,trainerUpPanel);
        });
    }
    //方法
    public JButton getBackBtn(){
        return trainerBottomRightPanel.getBackBtn();
    }


    public void isCorrect(){
        upData.setCorrect(upData.getCorrect()+1);
        trainerUpPanel.getCorrect().setText(""+upData.getCorrect());
        trainerBottomLeftPanel.getAnsJudge().setText("正确");
    }

    public void isWrong(){
        upData.setWrong(upData.getWrong()+1);
        trainerUpPanel.getWrong().setText(""+upData.getWrong());
        trainerBottomLeftPanel.getAnsJudge().setText("错误");
    }
    public void compare(int ans){
        if (Objects.equals(trainerData.getOpLabel(), "+")){
            if (ans==(trainerData.getOp1()+trainerData.getOp2())){   isCorrect(); }
            else{ isWrong(); }
        }
        else {
            if (Objects.equals(trainerData.getOpLabel(), "-")){
                if (ans==(trainerData.getOp1()-trainerData.getOp2())){   isCorrect();    }
                else{   isWrong(); }
            }
        }
    }
}
